var app = getApp();
Component({
    properties: {
        isform : Boolean,
        fee : Number,
        mainclass : String,
        shopid : Number,
    },
    data: {
        timer: null,
        time: 0,
        info : {},
        form : [],
        timeData : {},
        isshowmulti : false,
    },
    ready:function(){
        var self = this;
        if( self.data.isform ){
            app.com.http('index_v1','POST',{op:'addforminfo'},0,false,function(res){
                if (!res.data.errno) {
                    self.setData({
                        info : res.data.data,
                        form : res.data.data.form ? res.data.data.form : [],
                    });
                }
            });
        }
    },
    methods: {
        onChange(e){
            this.setData({
                timeData : e.detail
            })
        },
        formfn(e){
            var self = this;
            var index = e.currentTarget.dataset.index;
            var value = e.detail.value;
            for (var i = 0; i < self.data.form.length; i++) {
                if(i == index){
                    if( self.data.form[i].type == 'single' ){
                        value = self.data.form[i].sdata[value]
                    }
                    self.setData({
                        ['form['+i+'].value'] : value,
                    })
                }
            }
        },
        subform(){
            var self = this;
            var postdata = {
                op:'addform',
                shopid : self.data.shopid ? self.data.shopid : 0,
            };
            var subform = [];
            if( self.data.form ){
                for (var i = 0; i < self.data.form.length; i++) {   
                    if( !self.data.form[i].value ){
                        app.com.alert('请完善'+self.data.form[i].name);
                        return false;
                    }
                    subform.push({
                        name : self.data.form[i].name,
                        value : self.data.form[i].value,
                    });
                }
            }
            postdata.subform = subform ? JSON.stringify( subform ) : '';

            app.com.auth(function(){
                app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
                    if (res.data && !res.data.errno) {
                        app.com.alert(res.data.message,function(){
                            self.subsuc();
                        });
                    }else if(res.data.errno == 200){    
                        wx.requestPayment({
							'appId' : res.data.data.appId,
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            success: function (payres) {
                                if( payres.errMsg == 'requestPayment:ok' ){
                                    app.com.alert('已成功提交',function(){
                                        self.subsuc();
                                    });
                                }
                            },
                            fail: function (payres) {
                                
                            }
                        });
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            })
        },
        subsuc(){
            for (var i = 0; i < this.data.form.length; i++) {   
                this.setData({
                    ['form['+i+'].value'] : '',
                })
            }
        },
        showmulti(e){
            var self = this;
            var index = e.currentTarget.dataset.index;
            for (var i = 0; i < self.data.form.length; i++) {
                if(i == index){
                    self.setData({
                        actmultiindex : index,
                        isshowmulti : true,
                        multidata : self.data.form[i].sdata,
                    })
                }
            }
        },
        hideshowmulti(){
            this.setData({
                isshowmulti : false
            })
        },
        onChangemulti(e){
            this.setData({
                multiresult : e.detail
            })
        },
        comfirmmulti(){
            var e = {
                currentTarget : {
                    dataset : {
                        index : this.data.actmultiindex
                    }
                },
                detail : {
                    value : this.data.multiresult
                }
            };
            this.formfn(e);
            this.setData({
                multiresult : [],
                isshowmulti : false,
            })
        },
        location : function( e ){
            app.com.location( e );
        },
        other : function( e ){
            app.com.otherapp( e );
        },
        weburl : function(e){
            app.com.toweburl( e,this );
        },
        url : function( e ){
            app.com.navigateto( e );
        },
        tel : function(e){
            app.com.callphone( e );
        },
    }
});